
--[[
	**********************
	* SvFrame - thomasfn *
	**********************
	teleport.lua - Loads basic teleporting functionality
]]

function PLUGIN:InitTeleport()
	svr.AddFlag( "cantp" )
	
	self:Command( "tpto", self.TPTo, { FLAG_CANTP } )
	self:Command( "tphere", self.TPHere,  { FLAG_CANTP } )
	self:Command( "tp", self.TP,  { FLAG_CANTP } )
	
	svr.AliasCommand( "tp", "teleport" )
end

table.insert( PLUGIN.Inits, PLUGIN.InitTeleport )

function PLUGIN:TPTo( ply, args )
	if (not svr.CheckArg( args, 1, "string" )) then return ply:ChatPrint( colour.White, "Syntax: /tpto <name>" ) end
	local plist = svr.FindPlayers( args[1] )
	if (#plist == 0) then return ply:ChatPrint( colour.Red, "No players found with that name." ) end
	if (#plist > 1) then return ply:ChatPrint( colour.Red, "Multiple players found with that partial name." ) end
	self:Teleport( ply, plist[1] )
end

function PLUGIN:TPHere( ply, args )
	if (not svr.CheckArg( args, 1, "string" )) then return ply:ChatPrint( colour.White, "Syntax: /tphere <name>" ) end
	local plist = svr.FindPlayers( args[1] )
	if (#plist == 0) then return ply:ChatPrint( colour.Red, "No players found with that name." ) end
	if (#plist > 1) then return ply:ChatPrint( colour.Red, "Multiple players found with that partial name." ) end
	self:Teleport( plist[1], ply )
end

function PLUGIN:TP( ply, args )
	if (not svr.CheckArg( args, 1, "string" )) then return ply:ChatPrint( colour.White, "Syntax: /tp <name> <target>" ) end
	if (not svr.CheckArg( args, 2, "string" )) then return ply:ChatPrint( colour.White, "Syntax: /tp <name> <target>" ) end
	
	local plistA = svr.FindPlayers( args[1] )
	if (#plistA == 0) then return ply:ChatPrint( colour.Red, "No players found with that name. ('" .. args[1] .. "')" ) end
	if (#plistA > 1) then return ply:ChatPrint( colour.Red, "Multiple players found with that partial name. ('" .. args[1] .. "')" ) end
	
	local plistB = svr.FindPlayers( args[2] )
	if (#plistB == 0) then return ply:ChatPrint( colour.Red, "No players found with that name. ('" .. args[2] .. "')" ) end
	if (#plistB > 1) then return ply:ChatPrint( colour.Red, "Multiple players found with that partial name. ('" .. args[2] .. "')" ) end
	
	self:Teleport( plistA[1], plistB[1] )
	ply:ChatPrint( colour.Green, "Teleport successful." )
end

function PLUGIN:Teleport( ply, target )
	local pos = target.GetPosition()
	ply.SetPosition( pos )
	ply:ChatPrint( "You were teleported to ", target:GetColour(), target.Username, colour.White, "." )
	target:ChatPrint( ply:GetColour(), ply.Username, colour.White, " teleported to you." )
end